package com.example.mannortycoon.controller;

import com.example.mannortycoon.entity.UserEntity;
import com.example.mannortycoon.service.UserRelationShipService;
import com.example.mannortycoon.uitl.InvitationCodeUtil;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags="用户好友")
@RestController
@RequestMapping("/game/userRelationShip")
public class UserRelationShipController {

    private final UserRelationShipService userRelationShipService;

    @Autowired
    public UserRelationShipController(UserRelationShipService userRelationShipService){
        this.userRelationShipService = userRelationShipService;
    }

    @GetMapping("/getCode")
    @ApiOperation("用户生成好友邀请码")
    public R getCode(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        String code = InvitationCodeUtil.gen(userId);
        return R.ok().put("data",code);
    }

    @PostMapping("/addRelationShip/{code}")
    @ApiOperation("通过邀请码添加好友")
    public R addRelationShip(HttpServletRequest request, @PathVariable("code") String code){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userRelationShipService.addRelationShip(userId,code);
    }

    @PostMapping("/addRelationShip")
    @ApiOperation("通过搜索添加好友")
    public R addRelationShip(HttpServletRequest request,@RequestParam Long userId2){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userRelationShipService.addRelationShip(userId,userId2);
    }


    @GetMapping("/getRelationShipList")
    @ApiOperation("获取用户好友列表")
    public R getRelationShipList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<UserEntity> list = userRelationShipService.getRelationShipList(userId);
        return R.ok().put("data",list);
    }

    @PostMapping("/givePearl/{username}/{pearl}")
    @ApiOperation("根据用户名赠送珍珠")
    public R givePearl(@PathVariable("username") String username,@PathVariable("pearl") Double pearl,HttpServletRequest request){
        Long userId1 = JWTUtils.getId(request.getHeader("token"));
        return userRelationShipService.givePearl(userId1,username,pearl);
    }
}
